package atomicPlayground;


import static java.lang.Math.*;
import static mathUtils.VecMath.*;
import static processing.core.PConstants.*;
import userInterface.Clickable;
import processing.core.*;
import elec.ElectroLump;

public class ProtonLump extends ElectroLump implements Clickable {

	public float netCharge;
	public boolean isGrabbed;
	public PFont font;
	static public PFont smallFont;
	
	public ProtonLump(PApplet p, float c, float x, float y) {
		super(p, c, (float) sqrt(abs(c)), x, y);
	}
	public float electricRho() {return radius;}
	
	public void setCharge(float c) {
		charge = c;
		radius = (float) sqrt(abs(charge));
	}
	
	public boolean containsPoint(float x, float y) {
		float[] disp = position().clone();
		disp[0] -= x; disp[1] -= y;
		return (vecMag2(disp) < radius*radius);
	}
	public boolean containsPoint(float[] p) {
		return containsPoint(p[0], p[1]);
	}

	public Clickable mouseClickedIn(float x, float y) {
		Clickable retVal = null;
		if (containsPoint(x, y) && parent.keyPressed==true 
				&& parent.key==CODED && parent.keyCode==CONTROL) {				
			retVal = this;
			if (parent.mouseButton==LEFT) {
				setCharge(charge+1);
			}
			else if (parent.mouseButton==RIGHT) {
				setCharge(charge-1);
			}
		}
		return retVal;
	}

	public Clickable mousePressedIn(float x, float y) {
		Clickable retVal = null;
		if (containsPoint(x, y) && parent.keyPressed==false) {
			retVal = this;
			isGrabbed = true;
		}
		return retVal;
	}

	public void mouseReleased() {isGrabbed = false;}
	
	public void mouseDragged(float dx, float dy) {
		position()[0] += dx;
		position()[1] += dy;
	}

	public void draw() {
		parent.strokeWeight(0);
		int r=0, g=0, b=0;
		if (charge<0) {r = g = 250;}
		else {b = 250;}
		if (isGrabbed) {
			r = max(r, 150); g = max(g, 150); b = max(b, 150);
		}
		parent.fill(r,g,b);
		parent.stroke(r,g,b);
		parent.ellipse(position()[0], position()[1], radius*2, radius*2);
		
	}
	
	public void drawText(String text, int col, PFont f, float x, float y, int a, int ya) {
		parent.textMode(SCREEN);
		parent.textAlign(a, ya);
		parent.stroke(col);
		parent.fill(col);
		parent.textFont(f);
		float sx = parent.screenX(position()[0]+x, position()[1]+y);
		float sy = parent.screenY(position()[0]+x, position()[1]+y);
		parent.text(text, sx, sy);
	}
	
	public String chargeString(int c) {
		String retString;
		if (c==0) return "";
		if (c>0) retString = "+";
		else retString = "-";
		if (abs(c)>1) retString += abs(c);
		return retString;
	}
	
	public void drawCharge() {
		String text = "" + (int) charge;
		int col = parent.color(0,0,0);
		//if (charge>0) col = parent.color(0, 0, 250);
		//else if (charge<0) col = parent.color(250, 250, 0);		
		//else col = parent.color(0,0,0);
		drawText(text, col, font, 0, 0, CENTER, CENTER);		
	}
	
	public void drawNetCharge() {
		String text = chargeString((int) netCharge);
		int col;
		if (netCharge>0) col = parent.color(0, 250, 0);
		else if (netCharge<0) col = parent.color(250, 0, 0);		
		else col = parent.color(250,250,250);
		drawText(text, col, smallFont, 0, -radius, CENTER, TOP);
	}
}

